package cn.blnp.net.flowable.boot.ext.consumer;

import cn.blnp.net.flowable.boot.bean.dto.HistoricProcessInstanceEntityMessage;
import cn.blnp.net.flowable.boot.ext.producer.HistoricDataCompensationProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.persistence.entity.HistoricProcessInstanceEntity;
import org.flowable.engine.impl.persistence.entity.data.HistoricProcessInstanceDataManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * <h3></h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/26 11:21
 */
@Slf4j
@Component
@RocketMQMessageListener(
        topic = HistoricDataCompensationProducer.HISTORIC_TOPIC,
        consumerGroup = "historic_compensation_consumer_group"
)
public class HistoricDataCompensationConsumer implements RocketMQListener<HistoricProcessInstanceEntityMessage> {

    @Resource(name = "processEngineConfiguration")
    private ProcessEngineConfigurationImpl configuration;

    @Override
    public void onMessage(HistoricProcessInstanceEntityMessage entityMessage) {
        HistoricProcessInstanceDataManager processInstanceDataManager = configuration.getHistoricProcessInstanceDataManager();
        HistoricProcessInstanceEntity entity = processInstanceDataManager.findById(entityMessage.getEntity().getId());
        switch (entityMessage.getOpType()) {
            case INSERT:
                if (entity == null) {
                    processInstanceDataManager.insert(entityMessage.getEntity());
                }
                break;
            case UPDATE:
                if (entity != null && entity.getRevision() <= entityMessage.getEntity().
                        getRevision()) {
                    processInstanceDataManager.update(entityMessage.getEntity());
                }
                break;
            case DELETE:
                if (entity != null && entity.getRevision() <= entityMessage.getEntity().
                        getRevision()) {
                    processInstanceDataManager.delete(entityMessage.getEntity());
                }
                break;
            default:
                log.error("Unsupported opType.message={}", entityMessage);
        }
    }
}
